yjx.controller('homeController', ['$rootScope', '$scope', '$timeout', '$fetch', 'modalService', 'wxShareService', '$state', '$init', 'ipCookie','urlCgService','detailService', function ($rootScope, $scope, $timeout, $fetch, modalService, wxShareService, $state, $init, ipCookie,urlCgService,detailService) {
    $init.title("首页")
    // console.log("homeController====")
    /*test以下*/
    // function dd() {
    //     $timeout(function () {
    //         $rootScope.identityTo = 0;
    //         // $rootScope.inviterUserId = 4;
    //         // $rootScope.inviteTeamId =4;
    //         // var absurl = DOMAIN;
    //         detailService.openDetail('register');
    //         // detailService.openDetail('joinTeam');
    //         // urlCgService.encodeUrl(absurl);
    //     })
    // }

    // dd();    
    // /*test以上*/
    /**获取统计数据 */
    function getSumInfo() {
        $fetch.fetch(
            '/home/me/reportInfo',
            {
                cache: false
            }
        ).then(function (data) {
            $timeout(function () {
                $rootScope.mbrokerage = data.accumulativeBrokerage;
                $rootScope.mbrokerage1 = data.alsoCanGetBrokerage;
                $rootScope.myClients = data.customerCount;
            })
        })
    }
    if($rootScope.identity==1){
        getSumInfo();
     }
    /*获取banner数据*/
    function getBanners() {
        $fetch.fetch(
            '/activity/getBanners',
            {
                cache: false,
            }
        ).then(function (data) {
            $timeout(function () {
                if (data && data.banners && data.banners.length > 0) {
                    $scope.bannerList = data.banners;
                }
                $timeout(function(){
                    var bannerSwiper = new Swiper('#bannerSwiper', {
                        autoPlay: 3000,
                        speed: 2000,
                        autoplay: {
                            disableOnInteraction: false,
                        },
                        loop: true,
                        slidesPerView: 1.25,
                        centeredSlides: true,
                    })
                })
                
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg)
        })
    }

    getBanners();
    /*推荐产品分类*/
    function getRecProCat() {
        $fetch.fetch(
            '/home/dimension'

        ).then(function (data) {
            $timeout(function () {
                if (data) {
                    $scope.tags = data;
                }
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg)
        })
    }

    getRecProCat();
    /*获取推荐产品数据*/
    function getRecProduct() {
        $fetch.fetch(
            '/product/recommend/items',
            {
                cache: false,
            }
        ).then(function (data) {
            $timeout(function () {
                if (data && data.products) {
                    $scope.productList = data.products;
                }
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg)
        })
    }

    getRecProduct();
    /*获取合作机构数据*/
    var pageNum = 1,
        pageSize = 12,
        total;

    function getRecOrgs() {
        $fetch.fetch(
            '/settled/suppliers'
        ).then(function (data) {
            $timeout(function () {
                if (data && data.suppliers) {
                    $scope.orgList = data.suppliers;
                }
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg)
        })
    }

    getRecOrgs();

    /*获取精品课程*/
    function getCourses() {
        $fetch.fetch(
            '/article/course/',
            {
                cache: false
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.courList = data.courses
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }

    getCourses();
    $scope.bannerGo = function (item) {
        if (item.productList && item.productList.length == 1) {
            /*单个产品的活动直接打开产品详情页*/
            $state.go('productDetail', ({ id: item.productList[0].id }));
        } else {
            $state.go('bannerDetail', ({ id: item.id }))
        }
    }
    if (ipCookie('token')) {
        wxShareService.onMenuShareTimeline();
        wxShareService.onMenuShareAppMessage();
    }

    $scope.bannerDetail = function(item){
        if(item.description){
            $state.go('bannerDetail',{id:item.id})
        }
    }

}])